package vg.services.lib_manager;

import com.almworks.sqlite4java.SQLite;
import vg.services.config_manager.ConfigManager;

/**
 * Setups lib path for different products.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class LibManager {
    // Constants
    public static final String SQLITE_LIB_PATH = "SQLiteLibPath";
    public static final String SQLITE_LIB_DEFAULT_PATH = "lib/";

    // Mutex
    private static final Object generalMutex = new Object();

    // Main data
    private static LibManager instance = null;

    /**
     * Setups all paths
     */
    public void setupAllPaths() {
        synchronized (generalMutex) {
            String sqliteLibPath = getProperty(SQLITE_LIB_PATH);
            if (sqliteLibPath == null) {
                sqliteLibPath = SQLITE_LIB_DEFAULT_PATH;
                setProperty(SQLITE_LIB_PATH, sqliteLibPath);
            }
            SQLite.setLibraryPath(sqliteLibPath);
        }
    }

    public static LibManager getInstance() {
        synchronized (generalMutex) {
            if (instance == null) {
                instance = new LibManager();
            }
            return instance;
        }
    }

//==============================================================================
//------------------PRIVATE METHODS---------------------------------------------
    private static String getProperty(String prop) {
        ConfigManager configManager = ConfigManager.getInstance();

        if (configManager != null) {
            return configManager.getProperty(prop);
        }
        return null;
    }

    private static void setProperty(String prop, String value) {
        ConfigManager configManager = ConfigManager.getInstance();

        if (configManager != null) {
            configManager.setProperty(prop, value);
        }
    }
}
